package com.fc.h5dispaly.h5display.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class MyProvider extends ContentProvider {
    private Context mContext;
    DBHelper dbHelper = null;
    SQLiteDatabase database = null;
    private static final String AUTHORITY = "com.fc.h5dispaly.contentprovider";
    private static final int user_code = 1;
    private static final int job_code = 2;
    private static UriMatcher mMatcher = null;

    static {
        mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //  若URI资源路径 = content://cn.scu.myprovider/user ，则返回注册码User_Cod
        mMatcher.addURI(AUTHORITY, "user", user_code);
        mMatcher.addURI(AUTHORITY, "job", job_code);
    }

    @Override
    public boolean onCreate() {

        mContext = getContext();
        dbHelper = new DBHelper(mContext, "", null, 1);
        database = dbHelper.getWritableDatabase();
        // 初始化两个表的数据(先清空两个表,再各加入一个记录)
        database.execSQL("delete from user");
        database.execSQL("insert into user values(1,'Carson');");
        database.execSQL("insert into user values(2,'Kobe');");

        database.execSQL("delete from job");
        database.execSQL("insert into job values(1,'Android');");
        database.execSQL("insert into job values(2,'iOS');");

        return true;
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (mMatcher.match(uri)) {
            case user_code:
                tableName = DBHelper.USER_TABLE_NAME;
                break;
            case job_code:
                tableName = DBHelper.JOB_TABLE_NAME;
                break;
        }
        return tableName;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        String table = getTableName(uri);

//        query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
        return database.query(table, strings, s, strings1, null, null, s1, null);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        String table = getTableName(uri);// 根据URI匹配 URI_CODE，从而匹配ContentProvider中相应的表名
        database.insert(table,null,contentValues);
        // 当该URI的ContentProvider数据发生变化时，通知外界（即访问该ContentProvider数据的访问者
        mContext.getContentResolver().notifyChange(uri,null);
        return uri;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }
}
